layui.use(['table','form','layer'], function() {
	var table = layui.table,table2=layui.table,
	form = layui.form,layer=layui.layer,layer2=layui.layer;
    
    /*维度信息表格*/
    table.render({
		elem: '#tabdimension',
		id:"tabdimensioncon",
		url: '../dimensionmanagement/getdimensionlist',
		title: '量表维度信息表',
		skin: 'line',
		even: true,
		height:'250',
		cols: [
			[{
				type:"numbers",
				title:'序号',
				align:'center',
				width:120
			},{
				field: 'dimensionName',
				title: '维度名称',
				align:'center'
			}, {
				field: 'dimensionDescribe',
				title: '维度描述',
				align:'center'
			},{
				field: 'isDefault',
				title: '默认维度',
				align: 'center',
				toolbar: '#isdefaultdimensionbar'
			}, {
				title: '操作',
				toolbar: '#barDemo',
				align:'center',
				width:300
			}]
		],
		page: {
			layout: ['prev', 'page', 'next', 'skip', 'count', 'limit'],
			groups: 5,
			limit: 10,
			limits: [10, 20, 30, 40, 50],
			theme: '#1E9FFF',						
		},
	});
    
    /* 点击查询对维度信息进行筛选 */
    $("#btnseldimension").click(function(){
    	var dimensionname=$("#inpseldimension").val().trim();
    	table.render({
			elem: '#tabdimension',
			url: '../dimensionmanagement/getdimensionlist?dimensionname='+dimensionname,
			title: '量表维度信息表',
			skin: 'line',
			even: true,
			height:'250',
			cols: [
				[{
					type:"numbers",
					title:'序号',
					align:'center',
					width:120
				},{
					field: 'dimensionName',
					title: '维度名称',
					align:'center'
				}, {
					field: 'dimensionDescribe',
					title: '维度描述',
					align:'center'
				}, {
					field: 'isDefault',
					title: '默认维度',
					align: 'center',
					toolbar: '#isdefaultdimensionbar'
				},{
					title: '操作',
					toolbar: '#barDemo',
					align:'center'
				}]
			],
			page: {
				layout: ['prev', 'page', 'next', 'skip', 'count', 'limit'],
				groups: 5,
				limit: 10,
				limits: [10, 20, 30, 40, 50],
				theme: '#1E9FFF',						
			},
		});
    });
	
	/*维度信息---添加弹窗*/
	$("#btnadddimension").click(function(){
		$("#inpdimensionName").val("");
		$("#txtdimensionDescribe").val("");
		layer.open({
	        type: 1, 
	        title: '量表维度信息添加',
	        area: ['450px', '320px'],
	        btn : [ '保存'],
	        shade: 0.8,
	        content: $('#adddivdimension'),
	        yes : function() {
				var dimensionName=$("#inpdimensionName").val().trim();    //维度名称
				var dimensionDescribe=$("#txtdimensionDescribe").val().trim();  //维度描述
				if(dimensionName=="" || dimensionName==null){
					layer.msg("维度名称不能为空",{icon:2});
					return false;
				}
				if(dimensionDescribe=="" || dimensionDescribe==null){
					layer.msg("维度描述不能为空",{icon:2});
					return false;
				}
				$.ajax({
        			url: '../dimensionmanagement/adddimension',  
        	        type: 'get', 
        	        dataType: 'json',  
        	        data:{dimensionName:dimensionName,dimensionDescribe:dimensionDescribe},
        	        success: function (data) {
        	          if (data.code == 10001) {
        	        	  layer.confirm(data.msg, {
	    					  btn: ['确定'],
	    					  icon:1
	    				  }, function(){
	    					  layer.closeAll();
	    					  table.reload("tabdimensioncon", {
		    		                where: {
		    		                	keyword:data.code=='10001'
		    		                }
		    		          });	
	    				  });
        	          }else { 
        	        	  layer.alert(data.msg, {icon: 2});
        	          }  
        	        },
        	        error:function(){layer.alert(data.msg, {icon:2});}
        		});
			}
	    });
		
	});
	
    /*表格工具栏事件*/
    table.on('tool(tabdimension)', function(obj){
	    var data = obj.data;
	    //点击编辑
	    if(obj.event==='edit'){
	    	//将维度内容加载到输入框
			$("#inpdimensionName").val(data.dimensionName);
			$("#txtdimensionDescribe").val(data.dimensionDescribe);
			var dimensionid=data.dimensionid;
	    	layer2.open({
		        type: 1, 
		        title: '量表维度信息修改',
		        area: ['450px', '320px'],
		        btn : [ '保存'],
		        shade: 0.8,
		        content: $('#adddivdimension'),
		        yes : function() {
					var dimensionName=$("#inpdimensionName").val().trim();    //维度名称
					var dimensionDescribe=$("#txtdimensionDescribe").val().trim();  //维度描述
					if(dimensionName=="" || dimensionName==null){
						layer.msg("维度名称不能为空",{icon:2});
						return false;
					}
					if(dimensionDescribe=="" || dimensionDescribe==null){
						layer.msg("维度描述不能为空",{icon:2});
						return false;
					}
					$.ajax({
	        			url: '../dimensionmanagement/updatedimension',  
	        	        type: 'get', 
	        	        dataType: 'json',  
	        	        data:{dimensionName:dimensionName,dimensionDescribe:dimensionDescribe,dimensionid:dimensionid},
	        	        success: function (data) {
	        	          if (data.code == 10001) {
	        	        	  layer.confirm(data.msg, {
		    					  btn: ['确定'],
		    					  icon:1
		    				  }, function(){
		    					  layer.closeAll();
		    					  table.reload("tabdimensioncon", {
			    		                where: {
			    		                	keyword:data.code=='10001'
			    		                }
			    		          });	
		    				  });
	        	          }else { 
	        	        	  layer.alert(data.msg, {icon: 2});
	        	          }  
	        	        },
	        	        error:function(){layer.alert(data.msg, {icon:2});},
	        	       
	        		});
					
				}
		    });
	    }else if(obj.event==='addopt'){
	    	/*djf添加*/
			$("#inpoptiontitleid").val(data.dimensionid);
			$("#inpdimensionNametext").text(data.dimensionName);
			var index = layer.open({
		        type: 1, 
		        title: '选项信息添加',
		        area: ['850px', '550px'],
		        shade: 0.8,
		        content: $('#addoptionsion'),
		        maxmin: true,
		    });
	    	layer.full(index);
	    	
	    	/*选项信息列表-表格*/
	    	table.render({
				elem: '#taboptioncon',
				id:'taboptioncon',
				url: '../dimensionoption/getoptionbydimension?dimensionid='+data.dimensionid,
				title: '选项信息表表',
				skin: 'line',
				even: true,
				cols: [
					[{
						field: 'optionNumber',
						title: '选项序号'
					}, {
						field: 'optionContent',
						title: '选项内容'
					}, {
						field: 'optionScore',
						title: '选项分数'
					}, {
						title: '操作',
						toolbar: '#barDemooption'
					}]
				]
			});
	    }else if(obj.event==='delete'){
	    	layer.confirm('你确定要删除吗？', {
	    		  btn: ['确定','取消'] ,
	    		  icon:3
	    		}, function(){
	    	    	$.ajax({
	        			url: '../dimensionmanagement/deldimension',  
	        	        type: 'get', 
	        	        dataType: 'json',  
	        	        data:{dimensionid:data.dimensionid},
	        	        success: function (getdata) {
	        	          if (getdata.code == 10001) { 
	        	        	  layer.confirm(getdata.msg, {
		    					  btn: ['确定'],
		    					  icon:1
		    				  }, function(){
		    					  layer.closeAll();
		    					  table.reload("tabdimensioncon", {
			    		                where: {
			    		                	keyword:getdata.code=='10001'
			    		                }
			    		          });	
		    				  });
	        	          }else { 
	        	        	  layer.alert(getdata.msg, {icon: 2});
	        	          }  
	        	        },
	        	        error:function(){layer.alert(getdata.msg, {icon: 2});}
	        		});
	    		}, function(){}
	    	);
	    }
	    //点击维度标准分查询
	    else if(obj.event === 'dimensionsource'){
	    	$(".dimensioncoure-con").show();
	    	$("#checkdimensionid").val(data.dimensionid);
	    	$("#checkrole").text("当前维度："+data.dimensionName);
	    	document.getElementById("checkrole").innerHTML="当前维度："+data.dimensionName;
	    	table2.render({
				elem: '#tabdimensioncoure',
				id:"tab2dimensionsc",
				toolbar: '#toolbarDemo',
				url: '../dimensionmanagement/getdimensionscorelist?dimensionid='+data.dimensionid,
				title: '量表维度标准分',
				skin: 'line',
				even: true,
				cols: [
					[{
						field: 'dimensionLev',
						align: 'center',
						title: '维度等级',							
					},{
						field: 'startScore',
						align: 'center',
						title: '维度起始分'
					}, {
						field: 'endScore',
						align: 'center',
						title: '维度结束分'					
					},{
						field: 'resultDescription',
						align: 'center',
						title: '结果描述'
					},{
						field: 'expertAdvice',
						align: 'center',
						title: '专家建议'
					},{
						templet: '#dimencourtoolbar',
						align: 'center',
						title: '操作',
						width:150
					}]
				]
			});
	    	
	    	//量表维度标准分的添加
			table2.on('toolbar(tabdimensioncoure)', function(obj){
	   			var data = obj.data;
	   			if(obj.event==='coureadd'){
	   				$("#inpdimensionLev").val(""); 
					$("#inpstartScore").val("");    
					$("#inpendScore").val("");    
					$("#inpresultDescription").val("");
					$("#inpexpertAdvice").val("");  
					var index2=layer.open({
						type : 1,
						title : '维度标准分添加',
						area : [ '560px', '410px' ],
						shade : 0.4,
						content : $('#adddivdimensionstand'),
						btn : [ '保存', '返回' ],
						yes : function() {
							var dimensionid=$("#checkdimensionid").val().trim();  //维度id
							var dimensionLev=$("#inpdimensionLev").val().trim();   //维度等级
							var startScore=$("#inpstartScore").val().trim();      //维度起始分
							var endScore=$("#inpendScore").val().trim();          //维度结束分
							var resultDescription=$("#inpresultDescription").val().trim();   //结果描述
							var expertAdvice=$("#inpexpertAdvice").val().trim();         //专家建议
							if(dimensionLev=="" || dimensionLev==null){
								layer.msg("维度等级不能为空",{icon:2});
								return false;
							}
							if(startScore=="" || startScore==null){
								layer.msg("维度起始分不能为空",{icon:2});
								return false;
							}
							if(endScore=="" || endScore==null){
								layer.msg("维度结束分不能为空",{icon:2});
								return false;
							}
							if(resultDescription=="" || resultDescription==null){
								layer.msg("结果描述不能为空",{icon:2});
								return false;
							}
							if(expertAdvice=="" || expertAdvice==null){
								layer.msg("专家建议不能为空",{icon:2});
								return false;
							}
							$.ajax({
			        			url: '../dimensionmanagement/adddimensionscore',  
			        	        type: 'get', 
			        	        dataType: 'json',  
			        	        data:{dimensionid:dimensionid,dimensionLev:dimensionLev,startScore:startScore,
			        	        	endScore:endScore,resultDescription:resultDescription,expertAdvice:expertAdvice},
			        	        success: function (getdata) {
			        	          if (getdata.code == 10001) {
			        	        	  layer.confirm(getdata.msg, {
				    					  btn: ['确定'],
				    					  icon:1
				    				  }, function(){
				    					  layer.closeAll();
				    					  table.reload("tab2dimensionsc", {
					    		                where: {
					    		                	keyword:getdata.code=='10001'
					    		                }
					    		          });	
				    				  });
			        	          }else { layer.alert(getdata.msg, {icon: 2});}  
			        	        },
			        	        error:function(){layer.alert(getdata.msg, {icon:2});}
			        		});
						},
						btn2 : function() {layer.close(index2);}
					});
	   			}
	   		});
			
			/*维度标准分操作列*/
			table2.on('tool(tabdimensioncoure)', function(obj){
	   			var data = obj.data;
	   			$("#inpdimensionLev").val(data.dimensionLev);   //维度等级
				$("#inpstartScore").val(data.startScore);      //维度起始分
				$("#inpendScore").val(data.endScore);          //维度结束分
				$("#inpresultDescription").val(data.resultDescription);   //结果描述
				$("#inpexpertAdvice").val(data.expertAdvice); 
	   			if(obj.event==='coureedit'){
	   				var index3=layer.open({
						type : 1,
						title : '维度标准分信息修改',
						area : [ '560px', '410px' ],
						shade : 0.4,
						content : $('#adddivdimensionstand'),
						btn : [ '保存', '返回' ],
						yes : function() {
							var dimensionLev=$("#inpdimensionLev").val().trim();   //维度等级
							var startScore=$("#inpstartScore").val().trim();      //维度起始分
							var endScore=$("#inpendScore").val().trim();          //维度结束分
							var resultDescription=$("#inpresultDescription").val().trim();   //结果描述
							var expertAdvice=$("#inpexpertAdvice").val().trim();         //专家建议
							if(dimensionLev=="" || dimensionLev==null){
								layer.msg("维度等级不能为空",{icon:2});
								return false;
							}
							if(startScore=="" || startScore==null){
								layer.msg("维度起始分不能为空",{icon:2});
								return false;
							}
							if(endScore=="" || endScore==null){
								layer.msg("维度结束分不能为空",{icon:2});
								return false;
							}
							if(resultDescription=="" || resultDescription==null){
								layer.msg("结果描述不能为空",{icon:2});
								return false;
							}
							if(expertAdvice=="" || expertAdvice==null){
								layer.msg("专家建议不能为空",{icon:2});
								return false;
							}
							$.ajax({
			        			url: '../dimensionmanagement/updatedimensionscore',  
			        	        type: 'get', 
			        	        dataType: 'json',  
			        	        data:{scoreid:data.id,dimensionLev:dimensionLev,startScore:startScore,
			        	        	endScore:endScore,resultDescription:resultDescription,expertAdvice:expertAdvice},
			        	        success: function (getdata) {
			        	          if (getdata.code == 10001) {
			        	        	  layer.confirm(getdata.msg, {
				    					  btn: ['确定'],
				    					  icon:1
				    				  }, function(){
				    					  layer.closeAll();
				    					  table.reload("tab2dimensionsc", {
					    		                where: {
					    		                	keyword:getdata.code=='10001'
					    		                }
					    		          });	
				    				  });
			        	          }else { layer.alert(getdata.msg, {icon: 2})}
			        	        },
			        	        error:function(){layer.alert(getdata.msg, {icon:2});}
			        		});
							
						},
						btn2 : function() {layer.close(index3);}
					});
	   			}else if(obj.event==='couredelete'){
	   				layer.confirm('你确定要删除吗？', {
			    			btn: ['确定','取消'] ,
			    			icon:3
			    		}, function(){
			    			$.ajax({
			        			url: '../dimensionmanagement/deletedimensionscore',  
			        	        type: 'get', 
			        	        dataType: 'json',  
			        	        data:{scoreid:data.id},
			        	        success: function (getdata) {
			        	          if (getdata.code == 10001) { 
			        	        	  layer.confirm(getdata.msg, {
				    					  btn: ['确定'],
				    					  icon:1
				    				  }, function(){
				    					  layer.closeAll();
				    					  table.reload("tab2dimensionsc", {
					    		                where: {
					    		                	keyword:getdata.code=='10001'
					    		                }
					    		          });	
				    				  });
			        	          }else { 
			        	        	  layer.alert(getdata.msg, {icon: 2});
			        	          }  
			        	        },
			        	        error:function(){layer.alert(getdata.msg, {icon: 2});}
			        		});
			    		}, function(){}
			    	);
	   			}
	   		});
	    }
    });
    
    /*2019/3/5 djf添加*/
	/*点击添加,添加一条选项信息*/
	$("#btnaddaoption").click(function(){
		$("#inpoptionscore").val("");
		$("#inpoptioncontent").val("");
		var index2=layer.open({
			type : 1,
			title : '单个选项添加',
			area : [ '560px', '380px' ],
			shade : 0.4,
			content : $('#adddivaoption'),
			btn : [ '保存'],
			yes : function() {
				var seloptionchange=$("#seloptionchange").val();  //选项排序号（1、2、3）
				var seloptiontext=$("#seloptionchange").find("option:selected").text();  //选题名称A、B
				var inpoptionscore=$("#inpoptionscore").val().trim();  //选项分数
				var inpoptioncontent=$("#inpoptioncontent").val().trim();   //选项内容
				var optiondimensionid=$("#inpoptiontitleid").val();     //维度id
				var newcontent=seloptiontext+"、"+inpoptioncontent;
				if(seloptionchange==undefined || seloptionchange=="00"){
					layer.msg("请选择维度选项！",{icon:2});
					return false;
				}
				else if(inpoptionscore==null || inpoptionscore==""){
					layer.msg("请输入维度选项分数！",{icon:2});
					return false;
				}
				else if(inpoptioncontent==null || inpoptioncontent==""){
					layer.msg("请输入维度选项内容！",{icon:2});
					return false;
				}
				$.ajax({
        			url: '../dimensionoption/adddimensionoption',  
        	        type: 'get', 
        	        dataType: 'json',  
        	        data:{optionnumber: seloptionchange,optioncontent:newcontent,
        	        	optionscore:inpoptionscore,dimensionid:optiondimensionid},
        	        success: function (getdata) {
        	          if (getdata.code == 10001) { 
        	        	  var indexalert=layer.alert(getdata.msg, {icon:1}, function(){
        	        		  layer.close(indexalert);
	    					  layer.close(index2);
	    					  table.reload("taboptioncon", {
		    		                where: {
		    		                	keyword:getdata.code=='10001'
		    		                }
		    		          });	
	    				  });
        	          }else { 
        	        	  layer.alert(getdata.msg, {icon: 2});
        	          }  
        	        },
        	        error:function(){layer.alert(getdata.msg, {icon: 2});}
        		});
			}
		});
	});
    table.on('tool(taboptioncon)', function(obj){
	    var data = obj.data;
	    //点击删除
	    if(obj.event==='deleteoption'){
	    	layer.confirm('你确定要删除吗？', {
	    		  btn: ['确定','取消'] ,
	    		  icon:3
	    		}, function(){
	    	    	$.ajax({
	        			url: '../dimensionoption/deletedimensionoption',  
	        	        type: 'get', 
	        	        dataType: 'json',  
	        	        data:{optionid:data.optionid},
	        	        success: function (getdata) {
	        	          if (getdata.code == 10001) { 
	        	        	  var indexalertdel=layer.confirm(getdata.msg, {
		    					  btn: ['确定'],
		    					  icon:1
		    				  }, function(){
		    					  layer.close(indexalertdel);
		    					  table.reload("taboptioncon", {
			    		                where: {
			    		                	keyword:getdata.code=='10001'
			    		                }
			    		          });
		    				  });
	        	          }else { 
	        	        	  layer.alert(getdata.msg, {icon: 2});
	        	          }  
	        	        },
	        	        error:function(){layer.alert("删除失败", {icon: 2});}
	        		});
	    		}, function(){}
	    	);
	    }
    });
    
    
    //设置默认维度（在测评的时候该维度默认包含在内）T：是，F：不是
    form.on('checkbox(lockDemo)',function(obj){
    	var dimensionid = obj.value;
    	//默认
    	if(obj.elem.checked){
    		$.ajax({
    			type: 'get',
    			url: '../dimensionmanagement/enableOrDisEnableDimension?status=T&dimensionid='+dimensionid,
    			datatype: 'json',
    			success: function(data){
    				if(data.code == "1"){
    					layer.alert(data.msg,{icon:1});
    				}else{
    					layer.alert(data.msg,{icon:2});
    				}
    			},error: function(){layer.alert("设置失败！",{icon: 2});}
    		});
    	}else{
    		$.ajax({
    			type: 'get',
    			url: '../dimensionmanagement/enableOrDisEnableDimension?status=F&dimensionid='+dimensionid,
    			datatype: 'json',
    			success: function(data){
    				if(data.code == "1"){
    					layer.alert(data.msg,{icon:1});
    				}else{
    					layer.alert(data.msg,{icon:2});
    				}
    			},error: function(){layer.alert("设置失败！",{icon: 2});}
    		});
    	}
    });
    
    
});